package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.WendaComment;
import cn.wolfcode.luowowo.comment.query.WendaCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IWendaCommentRepository;
import cn.wolfcode.luowowo.comment.service.IWendaCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class WendalCommentServiceImpl implements IWendaCommentService {

    @Autowired
    private IWendaCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Override
    public WendaComment save(WendaComment comment) {
        if (!StringUtils.hasLength(comment.getId())) {
            comment.setCreateTime(new Date());
        }

        //处理引用评论
        String refId = comment.getRefComment().getId();
        if(StringUtils.hasLength(refId)) {
            //评论的评论
            WendaComment ref = repository.findById(refId).get();
            comment.setRefComment(ref);
        }
        repository.save(comment);
        return comment;
    }

    @Override
    public Page query(WendaCommentQuery qo) {
        //repository和template无法直接查询分页数据, 所以需要自己封装
        Query query = new Query();
        //拼接查询条件
        if (qo.getWendaId() != -1) {
            query.addCriteria(Criteria.where("wendaId").is(qo.getWendaId()));
        }
        //查总数
        long count = template.count(query, WendaComment.class);
        if (count == 0) {
            return Page.empty();
        }
        //查询当前页的list
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "createTime");
        List<WendaComment> list = template.find(query, WendaComment.class);
        return new PageImpl(list, pageable, count);
    }

    @Override
    public Long getCount(Long wendaId) {
        Query query = new Query();
        if (wendaId != null){
            query.addCriteria(Criteria.where("wendaId").is(wendaId));
        }
        //查询总数
        long count = template.count(query, WendaComment.class);
        return count;
    }

    @Override
    public Page queryByStatus(WendaCommentQuery qo) {
        //repository和template无法直接查询分页数据, 所以需要自己封装
        Query query = new Query();
        //拼接查询条件
        if (qo.getWendaId() != -1) {
            query.addCriteria(Criteria.where("wendaId").is(qo.getWendaId()));
        }
        //拼接查询条件
            query.addCriteria(Criteria.where("status").is(1));

        //查总数
        long count = template.count(query, WendaComment.class);
        if (count == 0) {
            return Page.empty();
        }
        //查询当前页的list
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "createTime");
        List<WendaComment> list = template.find(query, WendaComment.class);
        return new PageImpl(list, pageable, count);
    }
}
